package cn.netjava.robo3d.device;

import java.awt.Color;  
import java.awt.Graphics;  
import java.awt.Graphics2D;
  
import javax.swing.JLabel;  

import com.jme.math.FastMath;
  
/** 
 * 带下划线的label 
 * @author Administrator 
 * 
 */  
public class Mylabelinatlas extends JLabel{  
	
	private float w;
	private  float h;
	private int[] xPoints2;
	private int[] yPoints2;
	private float theta;
	
	private         float[] xPoints=new float[6];
    private		   float[] yPoints=new float[6];
    private         float[] xPoints3=new float[6];
    private		   float[] yPoints3=new float[6];
   
	 

      
    public Mylabelinatlas(int labelw,int labelh){  
    	super(); 
        setSize(labelw, labelh);
        w=getWidth();	                
        h=getHeight();
        xPoints2=new int[6];
        yPoints2=new int[6];
        xPoints[0]=w/9; xPoints[1]=3*w/9;xPoints[2]=w*8/9;xPoints[3]=3*w/9 ;xPoints[4]=w/9  ; xPoints[5]=3*w/9;
        yPoints[0]=h/4 ;yPoints[1]=h/4;yPoints[2]=h/2 ;yPoints[3]=3*h/4;yPoints[4]=3*h/4 ;yPoints[5]=h/2;
    }  
      
    
    public void update(float theta){
    	this.theta=theta;
    	repaint();
    }
      
    @Override  
    protected void paintComponent(Graphics g) {
    	//System.out.println("执行 paint");
        //super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.WHITE);
        
        //旋转
        for(int i=0;i<xPoints.length;i++)
        {
        	//原点变换
        	xPoints3[i]=xPoints[i]-w/2f;
        	yPoints3[i]=yPoints[i]-h/2f;
        	//旋转
            float[] xzi=myrotate(xPoints3[i], yPoints3[i], theta);
            //原点还原
            xPoints2[i]=(int)(xzi[0]+w/2f);
            yPoints2[i]=(int)(xzi[1]+h/2f);
        }
        g2d.fillPolygon(xPoints2, yPoints2,6);
        g2d.dispose();
    }  
    
	//绕原点旋转变换。为了保证精度用浮点型
	private float[] myrotate(float x,float z,float theta){
		float result[]=new float[2];
		result[0]=x*FastMath.cos(theta)-z*FastMath.sin(theta);
		result[1]=x*FastMath.sin(theta)+z*FastMath.cos(theta);
		return result;
	}
}  